package com.gujiangbo.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gujiangbo.entity.AcctUsers;
import com.gujiangbo.request.UpdateUserRequest;
import com.gujiangbo.request.UserInVo;
import com.gujiangbo.response.UserOutVo;
import com.gujiangbo.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserController {
    private final Log debugLog = LogFactory.getLog(UserController.class);
    private final ObjectMapper mapper = new ObjectMapper();

    @Resource
    private UserService userService;

    /**
     * 获取用户信息
     *
     * @param requestBody
     * @return
     * @throws IOException
     */
    @RequestMapping(value = {"/getUsers"}, method = {RequestMethod.POST}, produces = {"application/json;charset=UTF-8"})
    public String getUsers(@RequestBody String requestBody) throws IOException {
        String response;
        debugLog.info("request params:" + requestBody);
        UserInVo userInVo = mapper.readValue(requestBody, UserInVo.class);
        UserOutVo userOutVo = userService.queryUser(userInVo);
        response = JSONObject.toJSONString(userOutVo);
        debugLog.info("query result：" + response);
        return response;
    }

    /**
     * 批量新增用户
     * 3000一次请求
     *
     * @return
     */
    @RequestMapping(value = {"/batchUsers"}, method = {RequestMethod.POST}, produces = {"application/json;charset=UTF-8"})
    public String batchUsers() {
        debugLog.info("start batch insert users.");
        userService.batchUsers();
        return "insert success!";
    }

    /**
     * 更新用户信息- 只允许更新地址，手机号、用户名
     *
     * @param requestBody
     * @return
     */
    @RequestMapping(value = {"/updateUser"}, method = {RequestMethod.POST}, produces = {"application/json;charset=UTF-8"})
    public String updateUser(@RequestBody String requestBody) throws IOException {
        debugLog.info("request  params:" + requestBody);
        UpdateUserRequest updateUserRequest = mapper.readValue(requestBody, UpdateUserRequest.class);
        if (ObjectUtils.isEmpty(updateUserRequest.getUpdateUser())) {
            return "请求参数为空";
        }
        return userService.updateUser(updateUserRequest) ? "update success!" : "update fail!";
    }


    @RequestMapping(value = {"/queryUsersByMobiles"}, method = {RequestMethod.POST}, produces = {"application/json;charset=UTF-8"})
    public String queryUsersByMobiles(@RequestBody String requestBody) {
        debugLog.info("request  params:" + requestBody);
        AcctUsers acctUsers = userService.queryUsersByMobiles(requestBody);
        return acctUsers.toString();
    }
}
